package com.maserb.monitor.web.card;


import com.maserb.monitor.common.AjaxUtils;
import com.maserb.monitor.common.Func_T1;
import com.maserb.monitor.common.PageRequestParam;
import com.maserb.monitor.common.PageResponseResult;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.card.AttendanceRecordEntity;
import com.maserb.monitor.entity.schoolConduct.SchoolConductItemEntity;
import com.maserb.monitor.entity.schoolConduct.StudentConductRecordEntity;
import com.maserb.monitor.enums.SchoolAttendanceRecordTypeEnum;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.schoolStudent.AttendanceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 考勤后台接口
 */
@Controller
@RequestMapping("/attendanceRecord")
public class AttendanceRecordController {

    @Autowired
    private IToolService toolService;

    @Autowired
    private AttendanceRecordService service;

    @Autowired
    JwOrgService orgService;

    @Autowired
    UserService userService;


    @RequestMapping(value = {"/", "/index"})
    public ModelAndView getIndex(final HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        HttpSession session = request.getSession(true);
        UserEntity user = (UserEntity) session.getAttribute("user");
        String userId = user.getId();
        List<JwOrgEntity> clazzList = new ArrayList<JwOrgEntity>();
        if(null!=user){
            clazzList = orgService.findByUser(user);
        }
        SchoolAttendanceRecordTypeEnum[] typeEnums = SchoolAttendanceRecordTypeEnum.values();
        view.addObject("userId",userId);
        view.addObject("clazzList",clazzList);
        view.addObject("typeEnums",typeEnums);
        view.setViewName("card/attendance_index");
        return view;
    }

    @RequestMapping("/doListAttendance")
    @ResponseBody
    public PageResponseResult<AttendanceRecordEntity> doListAttendance(PageRequestParam page, String deviceId, String cardId, String studentId, String optType, String startTime) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<AttendanceRecordEntity>>() {
            @Override
            public Page<AttendanceRecordEntity> invoke(Pageable var1) throws Exception {
                Page<AttendanceRecordEntity> page = service.findAll(var1,deviceId,cardId,studentId,optType,startTime);
                for(AttendanceRecordEntity entity:page.getContent()){
                    /*entity.setStartTime(toolService.formatYYMMDDHHMMSS(entity.getStartTime()));*/
                }
                return page;
            }
        });
    }

    @RequestMapping("/doList")
    @ResponseBody
    public PageResponseResult<AttendanceRecordEntity> doList(PageRequestParam page,String userId, String name, String studentId, String clazzId, Integer optType, String startTime, String endTime) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "startTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<AttendanceRecordEntity>>() {
            @Override
            public Page<AttendanceRecordEntity> invoke(PageRequest var1) throws Exception {
                Page<AttendanceRecordEntity> page = service.findAll(var1,userId,name,studentId,clazzId,optType,startTime,endTime);
                for(AttendanceRecordEntity entity:page.getContent()){
                    if(!StringUtils.isEmptyOrWhitespace(entity.getStartTime())){
                        entity.setStartTime(toolService.formatYYMMDDHHMMSSTime(entity.getStartTime()));
                    }
                    if(null!=entity.getOptType()){
                        entity.setOptTypeCn(SchoolAttendanceRecordTypeEnum.getNameByValue(entity.getOptType()));
                    }
                }
                return page;
            }
        });
    }

}
